let equal = document.querySelector("#equal")
let express = document.querySelector("#express")
let result = document.querySelector("#result")
let ce = document.querySelector("#clearerror")
let c = document.querySelector("#clear")
let reverse = document.querySelector("#reverse")
let point = document.querySelector("#point")
let percent = document.querySelector("#percent")
let squareroot = document.querySelector("#squareroot")
let square = document.querySelector("#square")
let reciprocal = document.querySelector("#reciprocal")

let number = document.querySelectorAll(".number")
let sign = document.querySelectorAll(".sign")

let output1 = ""
let output2 = ""
let isNULL = true

equal.addEventListener("click", function (e) {
    if (output1 == "") {
        output1 = 0
    }
    result.value = eval(output2 + output1)
    if (result.value.length >= 11) {
        result.value = Number(result.value).toExponential(1)
    }
    output1 = result.value
    express.value = ""
}, false)

c.addEventListener("click", function (e) {
    result.value = "0"
    express.value = ""
    output1 = ""
    output2 = ""
}, false)
ce.addEventListener("click", function (e) {
    result.value = "0"
    output1 = ""
}, false)
reverse.addEventListener("click", function (e) {
    output1 = 0 - output1
    result.value = output1
}, false)
for (let index = 0; index < number.length; index++) {
    const element = number[index]
    element.addEventListener("click", function (e) {
        if (output2) {
            if (isNULL) {
                output1 = ""
            }
        }
        if (e.target.value == "0" && (output1.toString().indexOf(".") == -1)) {
            if (output1 == "" && !output2) {
                output1 = 0
            } else {
                if (output1 != "0") {
                    output1 += this.value
                }
            }
        } else {
            if (output1 == "0") {
                output1 = this.value
            } else {
                if (output1.length >= 11) {
                    output1 = output1.slice(0, 11)
                } else {
                    output1 += this.value
                    if (!/\+|-|×|÷/.test(express.value)) {
                        express.value = ""
                    }
                }
            }
        }
        isNULL = false
        if (output1.length >= 11) {
            output1 = output1.slice(0, 11)
        }
        result.value = output1
    }, false)
}

for (let index = 0; index < sign.length; index++) {
    const element = sign[index];
    element.addEventListener("click", function (e) {
        if (output1 == "") {
            output1 = 0
        }
        switch (e.target.value) {
            case "<":
                if (output1.length > 1) {
                    output1 = output1.substr(0, output1.length - 1)
                } else {
                    if (output1 != 0) {
                        output1 = 0
                    }
                }
                result.value = output1
                break
            case "÷":
                output2 = output1 + " / "
                express.value = output2
                break
            case "×":
                output2 = output1 + " * "
                express.value = output2
                break
            case "－":
                output2 = output1 + " - "
                express.value = output2
                break
            case "+":
                output2 = output1 + " + "
                express.value = output2
                break
            default:
                break
        }
    }, false)
}
point.addEventListener("click", function (e) {
    if (output1 == "") {
        output1 = "0"
    }
    if (output1.toString().indexOf(".") == -1) {
        output1 += "."
    }
    result.value = output1.toString()
}, false)
percent.addEventListener("click", function (e) {
    express.value = result.value + " %"
    result.value = result.value / 100
    result.value = Number(result.value).toFixed(2)
    output1 = result.value
}, false)
squareroot.addEventListener("click", function (e) {
    express.value = "√ " + result.value
    result.value = Math.sqrt(result.value)
    if (result.value.length >= 11) {
        result.value = Number(result.value).toFixed(5)
    }
    output1 = result.value
}, false)
square.addEventListener("click", function (e) {
    express.value = "sqrt(" + result.value + ")"
    result.value = result.value * result.value
    if (result.value.length >= 11) {
        result.value = Number(result.value).toExponential(1)
    }
    output1 = result.value
    output2 = express.value
}, false)
reciprocal.addEventListener("click", function (e) {
    if (output1 != 0) {
        express.value = "1/" + result.value
        result.value = eval(1 / result.value)
        if (result.value.length >= 11) {
            result.value = Number(result.value).toExponential(1)
        }
        output1 = result.value
    } else {
        result.value = "输入错误"
    }
}, false)